#include "QSD.h"

namespace QSDHANDLER
{
	QSD::QSD()
	{
		m_File = new FileHandler();
		m_Blocks = new LinkedList< Block >();	
	}

	QSD::~QSD()
	{
	}

	void QSD::Load( char* a_FilePath )
	{
		m_File->LoadFile( a_FilePath, "rb" );
	
		m_File->ReadInt( &m_FileVersion );
		m_File->ReadInt( &m_BlockCount );

		m_File->ReadSSTR( m_Name );

		for ( int i = 0; i < m_BlockCount; i++ )
		{
			Block* newBlock = new Block();
			newBlock->Load( m_File );
			m_Blocks->AddFront( newBlock );
		}


		m_BlocksIt = new Iterator< Block >( m_Blocks, 0 );
	}

	void QSD::Save( char* a_FilePath )
	{
		m_File->LoadFile( a_FilePath, "wb" );

		m_File->WriteInt( &m_FileVersion );
		m_File->WriteInt( &m_BlockCount );

		do 
		{
			m_BlocksIt->CurrentNode()->m_Data->Save( m_File );
		} while ( m_BlocksIt->Loop() == true );
	}

	void Block::Load( FileHandler* a_File )
	{
		a_File->ReadInt( &m_RecordCount );
		a_File->ReadSSTR( m_BlockName );

		for ( int i = 0; i < m_RecordCount; i++ )
		{
			Record* newRecord = new Record();
			newRecord->Load( a_File );
			m_Records->AddFront( newRecord );
		}
	}

	void Block::Save( FileHandler* a_File )
	{
		a_File->WriteInt( &m_RecordCount );
		//a_File->WriteSSTR( m_BlockName );

		do 
		{
			m_RecordsIt->CurrentNode()->m_Data->Save( a_File );
		} while ( m_RecordsIt->Loop() == true );
	}
	
	void Record::Load( FileHandler* a_File )
	{
		a_File->ReadByte( m_CheckNext );
		a_File->ReadInt( &m_ConditionCount );
		a_File->ReadInt( &m_ActionCount );
		a_File->ReadSSTR( m_RecordName );

		for ( int i = 0; i < m_ConditionCount; i++ )
		{
			Condition* newCondition = new Condition();
			newCondition->Load( a_File );
			m_Conditions->AddFront( newCondition );
		}

		m_ConditionsIt = new Iterator< Condition >( m_Conditions, 0 );

		for ( int i = 0; i < m_ActionCount; i++ )
		{
			Action* newAction = new Action();
			newAction->Load( a_File );
			m_Actions->AddFront( newAction );
		}

		m_ActionsIt = new Iterator< Action >( m_Actions, 0 );
	}

	void Record::Save( FileHandler* a_File )
	{
		a_File->WriteByte( m_CheckNext );
		a_File->WriteInt( &m_ConditionCount );
		a_File->WriteInt( &m_ActionCount );
		//a_File->WriteSSTR( m_RecordName );
	}

	void Condition::Load( FileHandler* a_File )
	{

	}

	void Condition::Save( FileHandler* a_File )
	{

	}

	void Action::Load( FileHandler* a_File )
	{

	}

	void Action::Save( FileHandler* a_File )
	{

	}
}
